﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Model.CSP;
using CSP.Common.Tools;
using CSP.Bll;
using CSP.Bll.IObjectBlls;
using CSP.Model.CSPEnum;

namespace CSP.Web.MyHome.ConsumeOrder
{
    public partial class OrderOK : CSPMyHome
    {
        private string _contactWayJson = "{}";

        private string _authenticationStyle = "";

        private string _authenticationName = "";

        private string _title = "";

        #region 属性
        /// <summary>
        /// 认证样式
        /// </summary>
        protected string AuthenticationStyle
        {
            get
            {
                return _authenticationStyle;
            }

        }

        /// <summary>
        /// 认证名称
        /// </summary>
        protected string AthenticationName
        {
            get
            {
                return _authenticationName;
            }
        }

        public string ContactWayJson
        {
            get
            {
                return _contactWayJson;
            }
        }

        /// <summary>
        /// 标题
        /// </summary>
        public string ServiceTitle
        {
            get {
                return _title;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageInit();
            }
        }

        private void PageInit()
        {
            if (Request["id"] == null)
            {
                LoggerProvider.Error("请求的Id不存在 /ConsumeOrder/OrderOK.aspx");
                
            }
            CSP.Model.CSP.ServiceInfo serviceInfo = GetServiceInfoById(Convert.ToInt32(Request["id"].ToString()));
            BindServiceInfoUI(serviceInfo);
            SetContactWay(serviceInfo.username);
        }

        /// <summary>
        /// 根据Id获取服务信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private CSP.Model.CSP.ServiceInfo GetServiceInfoById(int id)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            CSP.Model.CSP.ServiceInfo result = null;
            using (IServiceInfoBll serviceInfoBll = bllFactory.GetServiceInfoBll())
            {
                result = serviceInfoBll.GetDataById(id);
            }
            return result;
        }

        /// <summary>
        /// 绑定服务UI 信息
        /// </summary>
        /// <param name="serviceInfo"></param>
        private void BindServiceInfoUI(CSP.Model.CSP.ServiceInfo serviceInfo)
        {
            this.LB_Title.Text =_title= serviceInfo.title;
            this.LB_Price.Text = serviceInfo.Price.ToString("0.00");
            this.LB_OriginalPrice.Text = serviceInfo.OriginalPrice.Value.ToString("0.00");
            this.LB_PriceUnit.Text = serviceInfo.PriceUnit.name;
            this.LB_OriginalPriceUnit.Text = serviceInfo.OriginalPriceUnit.name;
            this.LB_Price.Text = serviceInfo.Price.ToString("0.00");
            this.LB_ClassOfMemberNumber.Text = serviceInfo.ClassOfMemberNumber.ToString();
            this.LB_UserName.Text = serviceInfo.username;

            this.LB_Province.Text = serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.CN_Province;
            this.LB_City.Text = serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.CN_City;
            this.LB_County.Text = serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.CN_County;
            this.LB_Street.Text = serviceInfo.ServiceInfoAddressRels.ToArray()[0].Address.Street;

            UserBasic userBasic = GetUserBasicInfoByUser(serviceInfo.username);
            _authenticationName = GetCNNameByAuthentication(userBasic.PersonalAuthentication, userBasic.EnterpriseAuthentication);
            _authenticationStyle = GetStyleByAuthentication(userBasic.PersonalAuthentication, userBasic.EnterpriseAuthentication);

        }

        /// <summary>
        /// 根据用户获取用户基本信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private UserBasic GetUserBasicInfoByUser(string userName)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();

            CSP.Model.CSP.UserBasic result = null;
            using (IUserInfoBll userInfoBll = bllFactory.GetUserInfoBll())
            {
                result = userInfoBll.GetUserBasicByUser(userName);
            }
            return result;
        }

        /// <summary>
        /// 根据User获取联系方式
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private ICollection<Model.CSP.ContactWay> GetContactWaysByUser(string userName)
        {
            ICollection<Model.CSP.ContactWay> result = null;
            CSPBllFactory factory = CSPBllFactory.GetInstance();
            using (IContactWayBll contactWayBll = factory.GetContactWayBll())
            {
                result = contactWayBll.FindByUser(userName);
            }

            return result;
        }

        /// <summary>
        /// 根据认证类型和认证状态获取 当前认证名称
        /// </summary>
        /// <param name="personalAuthentication"></param>
        /// <param name="enterpriseAuthentication"></param>
        /// <returns></returns>
        protected string GetCNNameByAuthentication(int personalAuthentication, int enterpriseAuthentication)
        {
            if ((AuthenticationStatus)enterpriseAuthentication == AuthenticationStatus.Accomplish)
            {
                return "企业认证";
            }

            if ((AuthenticationStatus)personalAuthentication == AuthenticationStatus.Accomplish)
            {
                return "个人认证";
            }

            return "未认证";
        }

        /// <summary>
        /// 获取样式根据企业认证和个人认证 状态
        /// </summary>
        /// <param name="personalAuthentication"></param>
        /// <param name="enterpriseAuthentication"></param>
        /// <returns></returns>
        protected string GetStyleByAuthentication(int personalAuthentication, int enterpriseAuthentication)
        {
            //企业认证
            if ((AuthenticationStatus)enterpriseAuthentication == AuthenticationStatus.Accomplish)
            {
                return "i-v-enterprise";
            }

            //个人认证
            else if ((AuthenticationStatus)personalAuthentication == AuthenticationStatus.Accomplish)
            {
                return "i-v-personal";
            }
            else
            {
                return "i-v-unautherized";
            }
        }

        /// <summary>
        /// 设置联系方式
        /// </summary>
        private void SetContactWay(string userName)
        {
            ICollection<Model.CSP.ContactWay> contactWays = GetContactWaysByUser(userName);

            if (contactWays == null || contactWays.Count == 0)
            {
                return;
            }

            ContactWayUI[] contactWayUI = (from contact in contactWays
                                           select new ContactWayUI
                                           {
                                               Address = contact.address,
                                               Type = contact.type
                                           }).ToArray();
            _contactWayJson = CommonTool.SerializeObject(contactWayUI);
        }


        private class ContactWayUI
        {
            public string Address { get; set; }

            public int Type { get; set; }
        }
    }
}